home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / SeqCltn.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  3KB  |  124 lines

  1. /* SeqCltn.c -- implementation of abstract sequential collections
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     September, 1985
  21.  
  22. Function:
  23.     
  24. SeqCltn is an abstract class representing collections whose elements are
  25. ordered and are externally named by integer indices.
  26.  
  27. $Log:    SeqCltn.c,v $
  28.  * Revision 3.0  90/05/20  00:21:12  kgorlen
  29.  * Release for 1st edition.
  30.  * 
  31. */
  32.  
  33. #include "SeqCltn.h"
  34.  
  35. #define    THIS    SeqCltn
  36. #define    BASE    Collection
  37. #define BASE_CLASSES BASE::desc()
  38. #define MEMBER_CLASSES
  39. #define VIRTUAL_BASE_CLASSES
  40.  
  41. DEFINE_ABSTRACT_CLASS(SeqCltn,0,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/SeqCltn.c,v 3.0 90/05/20 00:21:12 kgorlen Rel $",NULL,NULL);
  42.  
  43. extern const int NIHCL_RDABSTCLASS,NIHCL_INDEXRANGE;
  44.  
  45. SeqCltn::SeqCltn() {}
  46.  
  47. SeqCltn::SeqCltn(OIOifd& fd) : BASE(fd) {}
  48.  
  49. SeqCltn::SeqCltn(OIOin& strm) : BASE(strm) {}
  50.  
  51. int SeqCltn::compare(const Object& arg) const
  52. // Compare two sequenced collections.  If *this > arg return >0,
  53. // *this == arg return 0, and if *this < arg return <0.
  54. {
  55.     assertArgClass(arg,*SeqCltn::desc(),"compare");
  56.     Iterator i(*this);
  57.     Iterator j(castdown(arg));
  58.     Object*    p;    // pointer to next object in this SeqCltn
  59.     Object*    q;    // pointer to next object in arg SeqCltn
  60.  
  61.     while ((q = j++, p = i++)) {
  62. // previous elements compared equal; longer SeqCltn is therefore larger
  63.         if (q == 0) return 1;
  64. // compare() != 0 at any element determines ordering
  65.         int val;
  66.         if ((val = p->compare(*q)) != 0) return val;
  67.     }
  68. // all elements in this SeqCltn compare() equal to arg SeqCltn
  69.     if (q == 0) return 0;    // size() == arg.size()
  70.     return -1;
  71. }
  72.  
  73. Object* SeqCltn::first() const { return (Object*)at(0); }
  74.     
  75. unsigned SeqCltn::hash() const
  76. {
  77.     unsigned h = size();
  78.     DO(*this,Object,p) h ^= p->hash(); OD
  79.     return h;
  80. }
  81.  
  82. int SeqCltn::indexOf(const Object& ob) const
  83. {
  84.     int i = 0;
  85.     DO(*this,Object,p)
  86.         if (p->isEqual(ob)) return i;
  87.         i++;
  88.     OD
  89.     return -1;
  90. }
  91.     
  92. bool SeqCltn::isEqual(const Object& ob) const
  93. {
  94.     assertArgClass(ob,*SeqCltn::desc(),"isEqual");
  95.     if (size() != ob.size()) return NO;
  96.     Iterator i(*this);
  97.     Iterator j(castdown(ob));
  98.     Object* p;
  99.     while (p = i++) if (!p->isEqual(*(j++))) return NO;
  100.     return YES;
  101. }
  102.  
  103. const Class* SeqCltn::species() const    { return &classDesc; }
  104.  
  105. Object* SeqCltn::last() const { return (Object*)at(size()-1); }
  106.  
  107. unsigned SeqCltn::occurrencesOf(const Object& ob) const
  108. {
  109.     unsigned n = 0;
  110.     DO(*this,Object,p) if (p->isEqual(ob)) n++; OD
  111.     return n;
  112. }
  113.     
  114. Object* SeqCltn::doNext(Iterator& pos) const
  115. {
  116.     if (pos.index < size()) return (Object*)at(pos.index++);
  117.     return 0;
  118. }
  119.  
  120. void SeqCltn::indexRangeErr() const
  121. {
  122.     setError(NIHCL_INDEXRANGE,DEFAULT,this,className());
  123. }
  124.